package com.icez.base_util_library.activityResultContracts

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION
import android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import androidx.activity.result.contract.ActivityResultContract
import androidx.core.content.FileProvider
import androidx.core.content.contentValuesOf
import com.icez.base_util_library.FileUtil
import com.icez.base_util_library.SystemUtil
import java.io.File
import java.security.AuthProvider

/**
 *  拍照回调
 * 在Application xml中新增
 * <provider
 * android:name="androidx.core.content.FileProvider"
 * android:authorities="com.example.android.fileprovider"
 * android:exported="false"
 * android:grantUriPermissions="true">
 * <meta-data
 * android:name="android.support.FILE_PROVIDER_PATHS"
 * android:resource="@xml/file_paths" />
 * </provider>
 * 在rex 中新建 xml目录并新建file_paths.xml
 * <?xml version="1.0" encoding="utf-8"?>
 *     <paths xmlns:android="http://schemas.android.com/apk/res/android">
 *         <external-path name="root_path" path="." />
 *         </paths>
 *
 *  注意：
 *  1、.launch("指定的autharities名字");例如：“com.example.android”
 *
 *  2、拍照后需要立即获取到图片，请先扫描然后再查询
 *
 */
class TakePictureResultContracts: ActivityResultContract<String, Uri?>() {
    var uri:Uri ?= null
    var file:File ?= null
    var context:Context ?= null
    override fun createIntent(context: Context, authorityName: String): Intent {
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        this.context = context
        val displayName = context.packageName+"_"+System.currentTimeMillis()+".webp"

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
            val values = contentValuesOf(
                Pair(MediaStore.MediaColumns.DISPLAY_NAME,displayName),
                Pair(MediaStore.MediaColumns.MIME_TYPE,"image/*"),
                Pair(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
            )
            uri = context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values)
        }else{
            file = FileUtil.instance.createFile(Environment.DIRECTORY_PICTURES,displayName)
            uri = FileProvider.getUriForFile(context,authorityName+".fileprovider", file!!)
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT,uri)
        intent.flags = FLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION
        return intent
    }

    override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
        when(resultCode){
            Activity.RESULT_OK ->{
                if(Build.VERSION.SDK_INT < Build.VERSION_CODES.Q){
                    val scanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
                    scanIntent.data = Uri.fromFile(file)
                    context?.sendBroadcast(scanIntent)
                }
                return uri
            }
            else -> {
                return null
            }
        }
    }
}